/* eslint-disable */
/**
 * Created by GYL on 2018/8/24
 */
//项目班子搭建
import React, { Component } from 'react';
import Filtrate from '../../components/common/Filtrate';
import Container from "../../components/common/Container";
import MyTable from "../../components/common/MyTable";
import PublicService from "../../services/PublicService";
import Edit_Wrap from './sub/Edit_Wrap';
import Check_Wrap from './sub/Check_Wrap';
import request from '../../utils/request';
import MyPagination from "../../components/common/MyPagination";
import { message, Spin } from 'antd';
import moment from 'moment';
import Alter_Wrap from './sub/Alter_Wrap';
import style from './index.less';

class ProManage extends Component {

  state = {
    treeArrgonghao: [],
    AddData: [],
    treeArr: [],
    params: {},
    pageSize: 10,
    pageIndex: 1,
    total: 0,
    addModal: false,
    selectedRowKeys: [],
    dataSate: [],
    editWrap: false,//项目搭建页面开关
    checkWrap: false,//项目查看页面开关
    projectName: '',
    Alter_Wrap: false,//项目变更页面开关
    spinning: true,//页面loging开关你
    spinEdit: false,
    xmmc:[],
    ywjl:[],
    xmfzr:[],
  }
  componentDidMount() {
    this.onSubmit(1, 10)

    request({
      url: '/pm-web/projectmanage/baseuser/getUserTree',
      method: 'POST',
      data: {}
    }).then(data => {
      if (data.rc === 0) {
        this.setState({
          treeArr: data.ret
        })
      }

    })
    //获取节点树中含有工号
    request({
      url: '/pm-web/projectmanage/baseuser/getUserTree',
      method: 'POST',
      data: {}
    }).then(data => {
      if (data.rc === 0) {
        this.setState({
          treeArrgonghao: data.ret
        })
      }

    })
    this.dropDownBoxTwo()
  }

  dropDownBoxTwo = () => {
    request({
      url: '/pm-web/projectmanage/findData/dropDownBoxTwo',
      method: 'GET',
      params: {
        userId: PublicService.getCookie('userId')
      }
    }).then(data => {

      if (data.rc === 0) {
        this.setState({
          xmmc: data.ret.xmmc,
          ywjl: data.ret.ywjl,
          xmfzr: data.ret.xmfzr,
        })
      }
    })
  }


  //查询
  onSubmit = (page, size) => {
    const params = this.f1.getForm().getFieldsValue();
    params.page = page;
    params.rows = size;
    params.userId = PublicService.getCookie('userId'),
      this.setState({
        params,
        spinning: true,
        pageIndex: page,
        pageSize: size
      })

    request({
      url: '/pm-web/projectmanage/projectinfo/getAllProjectInfoTwo',
      method: 'POST',
      data: params
    }).then(data => {
      // console.log(data)
      if (data.rc === 0) {

        data.ret.items.forEach((item) => {
          item.winningBidDate = moment(item.winningBidDate).format("YYYY-MM-DD HH:mm:ss")
        })

        this.setState({
          dataSate: data.ret.items,
          // sbiList: data.ret.sbiList,
          total: data.ret.rowCount,
          spinning: false,
          selectedRowKeys: [],
        })
      }
    })
  }

  //重置
  ResetForm = () => {
    console.log('重置')
    this.setState({
      pageIndex: 1,
      pageSize: 10
    }, () => {
      this.onSubmit(1, 10)
    })
  }

  //新增
  addBtnText = () => {
    // console.log('新增')
    this.setState({
      editWrap: true
    })
  }

  // 取消保存
  ModalCancel = (type) => {
    let t = this;
    t.setState({
      [type]: false,
      modalBtnLoading: false
    })
  };

  //多选
  onSelectChange = (selectedRowKeys, row) => {
    console.log(selectedRowKeys, row)
    if (selectedRowKeys.length < 1) {
      this.setState({ selectedRowKeys, row })
    } else {
      this.setState({
        selectedRowKeys: [selectedRowKeys[selectedRowKeys.length - 1]],
        row: [row[row.length - 1]]
      })
    }

  };




  fanhui = (v) => {
    this.setState({
      [v]: false
    })
    setTimeout(() => {
      this.onSubmit(1, 10)
    })
  }



  /**
   * 编辑保存按钮
   */
  onModalSave = (v) => {
    // console.log(v)


    if (typeof v.projectLeader == 'string') {
      v.projectLeader = [v.projectLeader]
    }
    if (typeof v.technicalDirector == 'string') {
      v.technicalDirector = [v.technicalDirector]
    }
    request({
      url: '/pm-web/projectmanage/projectinfo/editProjectStaff',
      method: 'POST',
      data: { projectId: this.state.projectId, ...v }
    }).then(data => {
      if (data.rc === 0) {
        message.success("编辑成功!")
      } else {
        message.warning(data.err)
      }
      this.fanhui('editWrap')
    })

  }

  //编辑
  /*判断this.state.dataSate[this.state.selectedRowKeys[0]].configurationStatus == 1该值是否为1,
  为1时:进入项目班子搭建页面
  不为1时:说明项目已经搭建完成,进入项目班子变更页面
  */
  editBtn = () => {
    const {selectedRowKeys,dataSate} = this.state;
    if (selectedRowKeys.length !== 1) {
      message.warning('只能对单一事件编辑!')
      return
    }

    if (dataSate[selectedRowKeys[0]].configurationStatus == 1) {
      request({
        url: '/pm-web/projectmanage/button/judgeButtonIsOperation',//查询是否具有权限
        method: 'POST',
        data: { userId: PublicService.getCookie('userId'), buttonName: '班子搭建' }
      }).then(data => {
        if (data.rc === 0) {
          if (data.ret.type) {
            //直接进入编辑
            request({
              url: '/pm-web/projectmanage/projectinfo/getUserByProjectId',
              method: 'POST',
              data: { projectId: dataSate[selectedRowKeys[0]].projectId }
            }).then(data => {
              if (data.rc === 0) {
                this.setState({
                  editWrap: true,
                  AddData: data.ret,
                  projectId: dataSate[selectedRowKeys[0]].projectId,
                  projectName: dataSate[selectedRowKeys[0]].projectName
                })
              }
            })
          } else {
            message.warning("用户权限不足!")
          }
        }
      })
    } else {
      request({
        url: '/pm-web/projectmanage/button/judgeButtonIsOperation',//查询是否具有权限
        method: 'POST',
        data: { userId: PublicService.getCookie('userId'), buttonName: '班子变更' }
      }).then(data => {
        if (data.rc === 0) {
          if (data.ret.type) {
            request({
              url: '/pm-web/projectmanage/projectinfo/judgeWhetherChange',
              method: 'POST',
              data: { projectId: dataSate[selectedRowKeys[0]].projectId, userId: PublicService.getCookie('userId') }
            }).then(data => {

              if (data.rc === 0) {
                //项目已经搭建完成,且无审批信息
                this.setState({
                  Alter_Wrap: true
                })

              } else {
                //项目未搭建完成不可变更
                message.warning(`${data.err}`)

              }
            })
          } else {
            message.warning("用户权限不足!")
          }
        }
      })
    }
  }

  //查看
  /**
   * 查看项目班子搭建页面
   */
  examineBtn = () => {
    const {selectedRowKeys,dataSate} = this.state;
    if (selectedRowKeys.length !== 1) {
      message.warning('只能对单一事件查看!')
      return
    }

    request({
      url: '/pm-web/projectmanage/projectinfo/getUserByProjectId',
      method: 'POST',
      data: { projectId: dataSate[selectedRowKeys[0]].projectId }
    }).then(data => {
      if (data.rc === 0) {
        this.setState({
          checkWrap: true,
          AddData: data.ret,
          projectId: dataSate[selectedRowKeys[0]].projectId,
          projectName: dataSate[selectedRowKeys[0]].projectName
        })
      }

    })
  }
  /**
   * 返回上一级页面
   */
  onBack = () => {
    this.setState({
      Alter_Wrap: false
    })
  }

  onRef = (ref) => {
    this.child = ref
  }

  //导出
  exportBtn = (val) => {
    let t = this;
    let isAll = val === 'all' ? true : false;
    window.location.href = '/pm-web/projectmanage/projectinfo/exportProjectMessageTwo?' + PublicService.paramSerializer({
      ...t.state.params,
      isAll
    })
  };
  render() {
    let t = this;
    let width = document.body.clientWidth;
    // console.log(width)
    const height = document.body.clientHeight;
    let containerHeight = '83vh';
    let tableHeight = 280;
    if (width < 1920) {
      tableHeight = 300;
      containerHeight = '76vh';
    }
    const { pageSize, pageIndex, selectedRowKeys, total, dataSate,xmmc,ywjl,xmfzr } = this.state
    let rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange.bind(this),
    };

    const items = [
      {
        type: 'autoComplete',
        label: '项目名称',
        placeholder: '请输入 ..',
        paramName: 'projectName',
        options: xmmc,
        width:240
      },

      {
        type: 'autoComplete',
        label: '业务经理',
        placeholder: '请输入 ..',
        paramName: 'serviceManagerName',
        options: ywjl
      },
      {
        type: 'select',
        label: '搭建状态',
        paramName: 'status',
        options: [
          { text: '全部', value: '' },
          { text: '未搭建', value: '1' },
          { text: '已搭建', value: '2' },
        ],
      },
      {
        type: 'autoComplete',
        label: '项目负责人',
        placeholder: '请输入 ..',
        paramName: 'projectManagerName',
        options: xmfzr
      },
    ];

    const columns = [
      { title: '序号', dataIndex: 'num', width: 50 },
      { title: '项目名称', dataIndex: 'projectName', key: 'projectName', width: 200 },
      { title: '项目编号', dataIndex: 'projectNumber', key: 'projectNumber', width: 100 },
      { title: '业务经理', dataIndex: 'serviceManagerName', key: 'serviceManagerName', width: 100, align: 'center' },
      { title: '区域负责人', dataIndex: 'areaManager', key: 'areaManager', width: 100, align: 'center' },
      { title: '项目负责人', dataIndex: 'projectLeader', key: 'projectLeader', width: 100, align: 'center' },
      { title: '技术负责人', dataIndex: 'technicalDirector', key: 'technicalDirector', width: 100, align: 'center' },
      { title: '片区安全负责人', dataIndex: 'districtHead', key: 'districtHead', width: 100 },
      { title: '项目施工管理人员', dataIndex: 'constructionSupervision', key: 'constructionSupervision', width: 100 },
      { title: '项目技术支持', dataIndex: 'technicalSupport', key: 'technicalSupport', width: 100 },
      { title: '造价专员', dataIndex: 'costOfTheCommissioner', key: 'costOfTheCommissioner', width: 100 },
      { title: '招采部专员', dataIndex: 'forMiningCommissioner', key: 'forMiningCommissioner', width: 100 },
      { title: '设备工程师', dataIndex: 'equipmentEngineer', key: 'equipmentEngineer', width: 100 },
      { title: '搭建状态', dataIndex: 'setUpState', key: 'setUpState', width: 100 },
    ];
    return (
      <div style={{ position: 'relative', boxSize: 'border-box', padding: 5, paddingTop: 0 }} className={style.Warp_box}>
        {/* 变更 */}
        {
          this.state.Alter_Wrap &&
          <Alter_Wrap projectId={this.state.dataSate[this.state.selectedRowKeys[0]].projectId} onBack={this.onBack} />
        }
        {/* 编辑 */}

        {
          this.state.editWrap &&
          <Edit_Wrap
            onModalSave={this.onModalSave}
            onModalEdit={this.onRef}
            treeArr={this.state.treeArr}
            AddData={this.state.AddData}
            treeArrgonghao={this.state.treeArrgonghao}
            fanhui={this.fanhui.bind(this, 'editWrap')}
            projectName={this.state.projectName}
          />
        }

        {/* 查看 */}
        {
          this.state.checkWrap &&
          <Check_Wrap
            onModalSave={this.onModalSave}
            treeArr={this.state.treeArr}
            AddData={this.state.AddData}
            treeArrgonghao={this.state.treeArrgonghao}
            fanhui={this.fanhui.bind(this, 'checkWrap')}
            projectName={this.state.projectName}
          />
        }
        <Spin spinning={this.state.spinning} size='large'>
          <Filtrate
            items={items}
            ref={ref => { this.f1 = ref }}
            submit={this.onSubmit.bind(this, 1, 10)}
            ResetForm={this.ResetForm}
          />

          <Container
            style={{ height: containerHeight, position: "relative", overflow: 'auto' }}
            // header={true}
            editBtnShow={true}
            editBtn={this.editBtn}
            examineBtn={this.examineBtn}
            examineBtnShow={true}
            exportBtnShow={true}
            exportBtn={t.exportBtn.bind(t)}

          >
            <MyTable
              rowSelection={rowSelection}
              columns={columns}
              scroll={{ y: height - tableHeight, x: 1600 }}
              dataSource={PublicService.transformArrayData(dataSate, true, true, pageIndex, pageSize)}
              pagination={false}
            />
            <MyPagination
              showPage
              pageSize={pageSize}
              current={this.state.pageIndex}
              total={total}
              showSizeChanger
              showQuickJumper
              onChange={this.onSubmit}
              onShowSizeChange={this.onSubmit}
            />




          </Container>
        </Spin>
      </div>
    );


  }
}
export default ProManage;

